package com.runen.wnhz.runen.ui.adapter.puadapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.runen.wnhz.runen.R;
import com.runen.wnhz.runen.bean.UserMessageBean;
import com.runen.wnhz.runen.ui.OnItemClickListener;
import com.runen.wnhz.runen.ui.adapter.puadapter.UserMessageRecyclerAdapter.ViewHolder;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class UserMessageRecyclerAdapter extends RecyclerView.Adapter<ViewHolder> {


    private Context context;

    private List<UserMessageBean.DataBean.ListBean> listBeans;

    private OnItemClickListener mClickListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mClickListener = listener;
    }

    public UserMessageRecyclerAdapter(Context context, List<UserMessageBean.DataBean.ListBean> listBeans) {
        this.context = context;
        this.listBeans = listBeans;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.user_message_item_layout, viewGroup, false);
        return new ViewHolder(itemView,mClickListener);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        UserMessageBean.DataBean.ListBean listBean = listBeans.get(position);
        viewHolder.textMessageContent.setText(listBean.getDescribe());
        viewHolder.messageTitle.setText(listBean.getTitle());
        if("1".equals(listBean.getStatus())){
            viewHolder.imageNewMessage.setVisibility(View.GONE);
        }else{
            viewHolder.imageNewMessage.setVisibility(View.VISIBLE);
        }
        viewHolder.text_sendTime.setText(listBean.getSend_time());
    }

    @Override
    public int getItemCount() {
        if (listBeans != null) {
            return listBeans.size();
        } else {
            return 0;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.image_message_label)
        ImageView imageMessageLabel;
        @BindView(R.id.message_title)
        TextView messageTitle;
        @BindView(R.id.text_message_content)
        TextView textMessageContent;
        @BindView(R.id.image_newMessage)
        ImageView imageNewMessage;
        @BindView(R.id.text_sendTime)
        TextView text_sendTime;

        private OnItemClickListener mListener;// 声明自定义的接口

        public ViewHolder(View itemView,OnItemClickListener listener) {
            super(itemView);
            this.mListener = listener;
            ButterKnife.bind(this,itemView);
            // 为ItemView添加点击事件
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            // getpostion()为Viewholder自带的一个方法，用来获取RecyclerView当前的位置，将此作为参数，传出去
            mListener.onItemClick(view, getPosition());
        }
    }
}
